﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CameraSettingUC
{
    /// <summary>
    /// Camera setting form control
    /// </summary>
    public partial class CameraSettingForm : DevComponents.DotNetBar.Office2007Form
    {
        #region Attributs and Getter/Setter
        public event EventHandler eventSaveClick;
        public event EventHandler eventCancelClick;
        private string m_cameraIniPath;
        private bool m_runningCamera;
        private int m_imageNumber;
        #endregion

        public CameraSettingForm(string cameraIniPath)
        {
            InitializeComponent();
            m_cameraIniPath = cameraIniPath;
        }

        #region Logic function
        /// <summary>
        /// Initialize camera from init file
        /// </summary>
        private void initCamera()
        {
            m_imageNumber = 0;
            m_runningCamera = false;
            FVIL.DS.CFviGrabberParam loadparam = new FVIL.DS.CFviGrabberParam();
            loadparam.Load(m_cameraIniPath);
            cFviGrabberCamera.Controller.Create(loadparam);
            cFviGrabberCamera.Controller.ImageType = FVIL.ImageType.UC8;
            for (int index = 0; index < 3; index++)
                cFviGrabberCamera.Controller.Images.Add(new FVIL.Data.CFviImage());
            cFviGrabberCamera.Controller.Validate();
        }

        #endregion

        #region Events on form
        private void btnGrabContinue_Click(object sender, EventArgs e)
        {
            if (!m_runningCamera)
            {
                cFviGrabberCamera.Controller.Run();
            }
            else
            {
                cFviGrabberCamera.Controller.Pause();
            }
            m_runningCamera = !m_runningCamera;
        }

        private void btnGrabOne_Click(object sender, EventArgs e)
        {
            cFviGrabberCamera.Controller.Pause();
            m_runningCamera = false;
            m_imageNumber++;
            tbImageNumber.Text = m_imageNumber.ToString();
        }

        private void CameraSettingForm_Load(object sender, EventArgs e)
        {
            ivCamera.setImageView(cFviImageViewCamera, true);
            initCamera();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            OnSaveClick();
            this.Close();
        }

        public virtual void OnSaveClick()
        {
            EventHandler handler = eventSaveClick;
            if (handler != null)
            {
                handler(null, EventArgs.Empty);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            OnCancelClick();
            this.Close();
        }

        public virtual void OnCancelClick()
        {
            EventHandler handler = eventCancelClick;
            if (handler != null)
            {
                handler(null, EventArgs.Empty);
            }
        }

        private void cFviGrabberCamera_FrameValid(object sender, FVIL.DS.GrabberEventArgs e)
        {
            if (e.Status == FVIL.DS.GrabberEventStatus.Done)
            {
                try
                {
                    m_imageNumber++;
                    tbImageNumber.Text = m_imageNumber.ToString();
                    System.Threading.Thread.Sleep(Convert.ToInt32(tbCameraDelay.Text));
                    cFviImageViewCamera.Image = e.Image;
                    cFviImageViewCamera.Refresh();
                }
                catch
                {
                    return;
                }
            }
        }

        private void CameraSettingForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            cFviGrabberCamera.Controller.Stop();
            cFviGrabberCamera.Controller.Release();
        }
        #endregion


    }
}
